#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QDebug>
#include <virtualkeyboarddelegate.h>
#include <QSocketNotifier>
#include <QFile>
#include <QIODevice>
#include <windows.h>
#include <windowsx.h>
#include <eventeater.h>
#include <QQmlApplicationEngine>
#include <QQmlContext>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), boundaryWidth(4),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    VirtualKeyBoardDelegate *delgate = new VirtualKeyBoardDelegate(ui->quickWidget, ui->widget);
    this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    SetWindowLong((HWND)this->winId(), GWL_EXSTYLE, GetWindowLong((HWND)this->winId(), GWL_EXSTYLE) | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);

    ui->quickWidget->engine()->rootContext()->setContextProperty("mainwindow", this);


    EventEater *keyPressEater = new EventEater(this);
//    ui->quickWidget->installEventFilter(keyPressEater);
//    ui->pushButton->installEventFilter(keyPressEater);
//    ui->pushButton->setMouseTracking(true);
//    ui->pushButton->setStyleSheet("background-color: transparent");

    int screenWidth = 1920;
    int screenHeight = 1080;
//    this->setFixedSize(screenWidth, ui->widget->height());
//    this->setGeometry(0, screenHeight - this->height(), screenWidth, this->height());1
//    connect(ui->pushButton, &QPushButton::clicked, [this]() {
//        QFile fileout;
//        fileout.open(stdout, QIODevice::WriteOnly);
//        fileout.write(ui->lineEdit->text().toLatin1().constData(), ui->lineEdit->text().length()); // write to stderr
//        fileout.close();
//    });
}



MainWindow::~MainWindow()
{
    delete ui;
}


bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    MSG* msg = (MSG*)message;
    switch(msg->message)
        {
        case WM_NCHITTEST:
            int xPos = GET_X_LPARAM(msg->lParam) - frameGeometry().x();
            int yPos = GET_Y_LPARAM(msg->lParam) - frameGeometry().y();
            if(xPos < boundaryWidth && yPos<boundaryWidth)                    //左上角
                *result = HTTOPLEFT;
            else if(xPos>=width()-boundaryWidth&&yPos<boundaryWidth)          //右上角
                *result = HTTOPRIGHT;
            else if(xPos<boundaryWidth&&yPos>=height()-boundaryWidth)         //左下角
                *result = HTBOTTOMLEFT;
            else if(xPos>=width()-boundaryWidth&&yPos>=height()-boundaryWidth)//右下角
                *result = HTBOTTOMRIGHT;
            else if(xPos < boundaryWidth)                                     //左边
                *result =  HTLEFT;
            else if(xPos>=width()-boundaryWidth)                              //右边
                *result = HTRIGHT;
            else if(yPos<boundaryWidth)                                       //上边
                *result = HTTOP;
            else if(yPos>=height()-boundaryWidth)                             //下边
                *result = HTBOTTOM;
            else              //其他部分不做处理，返回false，留给其他事件处理器处理
               return false;
            return true;
        }
        return false;         //此处返回false，留给其他事件处理器处理
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton) {
        clickPos=e->pos();
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons()&Qt::LeftButton) {
        move(e->pos()+pos()-clickPos);
    }
}
